package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.ChooseBeiShuAdapter;
import com.lanlian.utillib.DisplayUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/22 20:09
 *     desc   : 选择合约
 *     version: 1.0
 * </pre>
 */
public class ChooseBeiShuDialog extends Dialog {

    private Context mContext;
    private TextView mTvCancel;
    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;

    private OnChooseContractListener mOnChooseContractListener;

    public interface OnChooseContractListener {
        void onChooseContract(Object obj);
    }

    public ChooseBeiShuDialog(Context context, OnChooseContractListener onChooseContractListener) {
        super(context, R.style.customDialog);
        this.mContext = context;
        this.mOnChooseContractListener = onChooseContractListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_contract);
        initView();
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;

        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i + 1 + "");
        }
        ChooseBeiShuAdapter adapter1 = new ChooseBeiShuAdapter(mContext, R.layout.adapter_choose_contract, list);

        mRecycleView.setAdapter(adapter1);
        adapter1.setChecked(list.get(0));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecycleView.setLayoutManager(linearLayoutManager);
        adapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                adapter1.setChecked(list.get(position));

                mOnChooseContractListener.onChooseContract(adapter.getItem(position).toString());
                dismiss();
            }
        });
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        resetRecycleViewHeight(mRecycleView);
    }

    private void initView() {
        TextView title = findViewById(R.id.tv_title);
        title.setText("选择倍数");
        mTvCancel = findViewById(R.id.tv_cancel);
        mLayRefresh = findViewById(R.id.lay_refresh);
        mRecycleView = findViewById(R.id.recycle_view);
    }

    private void resetRecycleViewHeight(RecyclerView recyclerView) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter == null) {
            return;
        }

        ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
        params.height = DisplayUtil.dip2px(mContext, 45) * 8;
        recyclerView.setLayoutParams(params);
    }

    public void showDialog() {
        Window w = getWindow();
        w.setWindowAnimations(R.style.animFromBottom);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        show();
    }

}
